<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\BasicException;
use App\Response\Response;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;                //引用http请求类

class CrossRoomPkBlackController extends BaseController
{
    /**
     * @api {get} /cross_room_pk/black/list  跨房PK黑名单列表
     * @apiVersion 0.1.0
     * @apiName 跨房PK黑名单列表
     * @apiGroup 跨房PK
     * @apiParam {number} room_id  房间id，非必传
     * @apiParam {number} perPage  每页数，非必传
     * @apiParam {number} page  页码，非必传
     * @apiParam {date} start_at  添加时间开始，非必传
     * @apiParam {date} end_at  添加时间结束，非必传
     * @apiParam {string} room_name  发起PK房间名，非必传
     */
    public function blackList(Request $request)
    {
        $param = $request->only(['room_id', 'room_name', 'page_size', 'page', 'start_at', 'end_at']);
        //参数校验
        $v = app('validator')->make(
            $param,
            [
                'room_id' => "filled|numeric",
                'room_name' => 'filled|string',
                'page_size' => 'filled|numeric',
                'page' => 'filled|numeric',
                'start_at' => 'date',
                'end_at' => 'date',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);

        $page = $param['page'] ?? 1;
        $perPage = $param['page_size'] ?? 10;
        $param['start_at'] = $param['start_at'] ?? '1970-01-01 00:00:00';
        $param['end_at'] = $param['end_at'] ?? '9999-01-01 00:00:00';

        $list = app('repo_cross_room_pk_black')->list($param, $page, $perPage);

        return Response::sendList($list);
    }

    /**
     * @api {get} /cross_room_pk/black/info  跨房PK黑名单-房间详情
     * @apiVersion 0.1.0
     * @apiName 跨房PK黑名单-房间详情
     * @apiGroup 跨房PK
     * @apiParam {number} room_id  房间id，必传
     *
     * @apiSuccessExample 成功响应
     *
     * {
    "code": 200,
    "data": {
    "user_id": 303330,
    "room_id": "441",
    "room_name": "霸气侧漏",
    "sociaty_name": "可口可乐"
    },
    "message": ""
    }
     **/
    public function info(Request $request)
    {
        $param = $request->only(['room_id']);
        $v = app('validator')->make($param, [
            'room_id' => 'required|numeric',
        ]);
        if ($v->fails()) throw new BasicException(10001);

        $info = app('repo_cross_room_pk_black')->info($param['room_id']);
        return Response::sendData($info);
    }

    /**
     * @api {post} /cross_room_pk/black/create  跨房PK黑名单-新增
     * @apiVersion 0.1.0
     * @apiName 跨房PK黑名单-新增
     * @apiGroup 跨房PK
     * @apiParam {number} room_id  房间id，必传
     * @apiParam {string} reason  房间id，非必传 200以内
     **/
    public function create(Request $request)
    {
        $param = $request->only(['room_id', 'reason']);
        //参数校验
        $v = app('validator')->make(
            $param,
            [
                'room_id' => "required|numeric",
                'reason' => 'filled|string|max:200',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);

        $param['admin_id'] = $request->admin_id;        //拿到admin_id
        $info = app('repo_cross_room_pk_black')->create($param);
        return Response::sendData($info);
    }

    /**
     * @api {post} /cross_room_pk/black/delete  跨房PK黑名单-移除
     * @apiVersion 0.1.0
     * @apiName 跨房PK黑名单-移除
     * @apiGroup 跨房PK
     * @apiParam {number} id  主键id，必传
     **/
    public function delete(Request $request)
    {
        $param = $request->only(['id']);
        //参数校验
        $v = app('validator')->make(
            $param,
            [
                'id' => "required|numeric|exists:mysql.ln_cross_room_pk_blacklist,id",
            ]
        );
        if ($v->fails()) throw new BasicException(10001);

        $param['admin_id'] = $request->admin_id;        //拿到admin_id
        app('repo_cross_room_pk_black')->delete($param);
        return Response::sendData(true);
    }
}
